﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyAspNetCoreTodo.Identity;
using MyAspNetCoreTodo.Models;

namespace MyAspNetCoreTodo.Controllers
{
    [Authorize(Roles = Constants.AdministratorRole)]
    public class ManageUsersController : Controller
    {

        private readonly UserManager<ApplicationUser> _userManger;

        public ManageUsersController(UserManager<ApplicationUser> userManger)
        {
            _userManger = userManger;
        } 
        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var admins = (await _userManger.GetUsersInRoleAsync(Constants.AdministratorRole)).ToArray();

            Dictionary<string, string[]> roles = new Dictionary<string, string[]>();
             

            var everyone = await _userManger.Users
              .ToArrayAsync();

            var model = new ManageUsersViewModel
            {
                Administrators = admins,
                Everyone = everyone
                ,UserRoles = roles
            };

            for (int i = 0; i < everyone.Length; i++)
            {

             var  role =   await  _userManger.GetRolesAsync(everyone[i]);
                roles[everyone[i].Id] = role.ToArray();
            }

            return View(model);
        }
        
    }
}
